<template>
  <view class="viewport">
    <!-- 地址列表 -->
    <scroll-view scroll-y>
      <view class="address">
        <!-- 按组使用 -->
        <uni-swipe-action>
          <uni-swipe-action-item
            v-for="item in addressList"
            :key="item.id"
            class="swipe-cell"
          >
            <view class="item" @click="selectedAddress(item)">
              <view class="user">
                {{ item.receiver }}
                <text>{{ item.contact }}</text>
                <text v-if="item.isDefault === 1" class="badge"> 默认 </text>
              </view>
              <view class="locate">
                {{ item.fullLocation }}{{ item.address }}
              </view>
              <!-- 🐛 添加阻止冒泡 -->
              <navigator
                :url="`./form?id=${item.id}`"
                class="edit"
                hover-class="none"
              >
                修改
              </navigator>
            </view>
            <template v-slot:right>
              <view class="swipe-cell-action">
                <button @tap="onAddressRemove(item.id)" class="delete-button">
                  删除
                </button>
              </view>
            </template>
          </uni-swipe-action-item>
        </uni-swipe-action>
      </view>
    </scroll-view>
    <!-- 添加按钮 -->
    <view class="add-btn">
      <navigator hover-class="none" url="./form">新建地址</navigator>
    </view>
  </view>
</template>

<script>
import { getMemberAddress, deleteMemberAddress } from "@/api/address";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      //地址列表
      addressList: [],
    };
  },
  // onLoad改为onShow修改返回才可以再次刷新,onLoad只会加载一次
  async onShow() {
    // 获取地址列表数据
    this.loadData();
  },
  methods: {
    ...mapMutations("address", ["setSelectedAddress"]),
    async loadData() {
      // 获取地址列表数据
      const res = await getMemberAddress();
      this.addressList = res.result;
    },
    //删除地址
    async onAddressRemove(id) {
      // 删除业务逻辑
      // 1.弹出模态框
      // 2.用户点击确定发送请求删除地址
      // 3.重新发送获取地址列表数据刷新页面
      //   3.1删除data中地址列表的数据
      //   3.2发请求重新获取地址列表数据

      // uni使用await有返回值的话为一个数组
      const [, { confirm }] = await uni.showModal({
        title: "提示信息",
        content: "你确定要删除吗?",
      });
      //   如果confirm为true代表点击了确定
      if (confirm) {
        //发送请求删除数据
        const res = await deleteMemberAddress(id);
        //提示用户删除成功
        await uni.showToast({
          title: "删除成功",
          duration: 1500,
        });
        setTimeout(() => {
          //重新获取地址列表数据
          this.loadData();
        }, 1000);
      }
    },
    //选择收获地址
    selectedAddress(item) {
      this.setSelectedAddress(item); //调用mutation

      //处理小bug只有从填写订单页面跳过来点击地址才返回上一页,从设置我的收获地址点击地址不会返回上一页
      // 填写订单页面跳转的时候代了参数form=order,但是只有onLoad可以获取,我们用的是onShow无法获取
      // 只能采用 getCurrentPages获取来源页面
      const pages = getCurrentPages();
      // console.log("pages  ----->  ", pages);
      const lastPage = pages[pages.length - 1];
      // console.log("  lastPage----->  ", lastPage);
      const { from } = lastPage.options;
      // console.log("from  ----->  ", from);
      if (from) {
        // 如果form有值代表是从订单页面跳转可以返回上一页
        //返回上一页
        uni.navigateBack();
      }
    },
  },
};
</script>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}
.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f4f4f4;
  scroll-view {
    padding-top: 20rpx;
  }
}
.address {
  padding: 0 20rpx;
  margin: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;
  .item {
    line-height: 1;
    padding: 40rpx 10rpx 38rpx;
    border-bottom: 1rpx solid #ddd;
    position: relative;
    .user {
      font-size: 28rpx;
      margin-bottom: 20rpx;
      color: #333;
      text {
        color: #666;
      }
      .badge {
        display: inline-block;
        padding: 4rpx 10rpx 2rpx 14rpx;
        margin: 2rpx 0 0 10rpx;
        font-size: 26rpx;
        color: #27ba9b;
        border-radius: 6rpx;
        border: 1rpx solid #27ba9b;
      }
    }
    .locate {
      line-height: 1.6;
      font-size: 26rpx;
      color: #333;
    }
    .edit {
      position: absolute;
      top: 36rpx;
      right: 30rpx;
      padding: 2rpx 0 2rpx 20rpx;
      border-left: 1rpx solid #666;
      font-size: 26rpx;
      color: #666;
      line-height: 1;
    }
  }
  .swipe-cell {
    &:last-child {
      .item {
        border: none;
      }
    }
  }
}
.swipe-cell-action {
  height: 100%;
  .delete-button {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 50px;
    height: 100%;
    font-size: 28rpx;
    color: #fff;
    border-radius: 0;
    padding: 0;
    background-color: #cf4444 !important;
  }
}
.add-btn {
  height: 80rpx;
  text-align: center;
  line-height: 80rpx;
  margin: 30rpx 20rpx;
  color: #fff;
  border-radius: 80rpx;
  font-size: 30rpx;
  background-color: #27ba9b;
}
.blank {
  margin-top: 300rpx;
  text-align: center;
  font-size: 32rpx;
  color: #888;
}
</style>
